﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using GraphTools;
using GraphTools.OSM;
using System.Collections;

namespace clcSimplifier
{
    public partial class Form1 : Form
    {
        protected string inputFile;
        protected string outputFile;    
    
        protected IDictionary<int, OSMNode> nodes;
        protected IDictionary<int, OSMWay> ways;
        protected IDictionary<int, OSMRelation> relations;

        public string InputFile
        {
            get{ return inputFile;}
            set{
                inputFile = value;
                inputFileBox.Text = value;
            }
        }

        public string OutputFile
        {
            get { return outputFile; }
            set {
                outputFile = value;
                outputFileBox.Text = value;
            }
        }
        

        public Form1()
        {
            InitializeComponent();
        }

        private void ChooseInputButton_Click(object sender, EventArgs e)
        {
            if (OFD1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                InputFile = OFD1.FileName;
            }
        }

        private void ChooseOutputButton_Click(object sender, EventArgs e)
        {
            if (SFD1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                OutputFile = SFD1.FileName;
            }
        }

        private void startButton_Click(object sender, EventArgs e)
        {
            logBox.Clear();
            if (String.IsNullOrEmpty(InputFile))
            {
                logBox.AppendText("Error: Bad input file.\n");
                return;
            }
            if (String.IsNullOrEmpty(OutputFile))
            {
                logBox.AppendText("Error: bad output file.\n");
                return;
            }
            logBox.AppendText(DateTime.Now.ToLongTimeString() + ": Start job...\n");
            logBox.AppendText(DateTime.Now.ToLongTimeString() + ": Parsing input...\n");

            OSMHandler.ReadFile(inputFile, out nodes, out ways, out relations, processRelations: false);
            OSMWay.SetNextID((ways.Keys.Max()+1));

            IDictionary<int,OSMWay> newWays;
            IDictionary<string, OSMRelation> newRelations;

            logBox.AppendText(DateTime.Now.ToLongTimeString() + ": Processing data...\n");
            Worker.DoWork(nodes, ways, out newWays, out newRelations);
            logBox.AppendText("Filtered ways: "+newWays.Count+"\n");

            resultlabel.Text = "Total: nodes="+nodes.Count+" ways="+ways.Count+" relations="+relations.Count;

            logBox.AppendText(DateTime.Now.ToLongTimeString() + ": Writing ourput...\n");
            OSMHandler.WriteFile(outputFile, nodes.Values, newWays.Values, newRelations.Values);
            logBox.AppendText(DateTime.Now.ToLongTimeString() + ": Job done.\n");
        
        
        }
    }
}
